//
//  DeviceListCell.m
//  efairy
//
//  Created by 李锋 on 2019/1/11.
//  Copyright © 2019年 East. All rights reserved.
//

#import "DeviceListCell.h"
#import "EFAModel.h"






@implementation DeviceListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


+(NSString *)getDeviceState:(int) state{
    // 设备状态，具体状态参考《网络传输协议》设备状态，0-离线 1-火警 2-预警 3-故障 4-启动 5-屏蔽 6-正常
    switch (state) {
        case 0:
            return @"离线";
            break;
        case 1:
             return @"火警";
            break;
        case 2:
             return @"预警";
            break;
        case 3:
             return @"故障";
            break;
        case 4:
             return @"启动";
            break;
        case 5:
             return @"屏蔽";
            break;
        case 6:
             return @"正常";
            break;
            
        default:
            break;
    }
    
     return @"未知";
}


+(int)getDeviceStateColor:(int) state{
    switch (state) {
        case 1:
            return 0xFF0000;
            break;
        case 2:
        case 3:
            return 0xFFB900;
            break;
        case 6:
            return 0x3FB059;
            break;
        default:
            break;
    }
    return 0xBBBBBB;
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self initView];
    }
    return self;
}


-(void)initView{
    _deviceIcon = [[UIImageView alloc]init];//设备图标
    _deviceIcon.image=[UIImage imageNamed:@"msg_device_icon"];
    _deviceName = [[UILabel alloc]init];//设备名字
    _deviceName.font = [UIFont systemFontOfSize:14.0];
    _deviceName.textColor= ColorFromRGB(0x333333);
    _deviceName.textAlignment = UIFontWeightBold;
    
    _deviceState = [[UILabel alloc]init];//设备状态
    _deviceState.font = [UIFont systemFontOfSize:12.0];
    _deviceState.textAlignment = UIViewContentModeRight;
    
    
    
    _deviceUUIDTitle = [[UILabel alloc]init];//设备uuid title
    _deviceUUIDTitle.font = [UIFont systemFontOfSize:14.0];
    _deviceUUIDTitle.textColor = ColorFromRGB(0x666666);
    
    _deviceUUID = [[UILabel alloc]init];//设备uuid
    _deviceUUID.font = [UIFont systemFontOfSize:14.0];
    _deviceUUID.textColor = ColorFromRGB(0x999999);
    
    _deviceOnlineIcon= [[UIImageView alloc]init];//是否在线
    
    _deviceOnline = [[UILabel alloc]init];//是否在线
    _deviceOnline.font=[UIFont systemFontOfSize:12.0];
    
    _signalLevelIcon = [[UIImageView alloc]init];//信号强度
    
    _signalLevel = [[UILabel alloc]init];//信号强度
    _signalLevel.font = [UIFont boldSystemFontOfSize:12.f];
    _signalLevel.textColor =ColorFromRGB(0x999999);
    
    
  
    
    [self.contentView sd_addSubviews:@[_deviceIcon,_deviceName,_deviceOnlineIcon,_deviceOnline,_deviceState,
                                       _deviceUUIDTitle,_deviceUUID,_signalLevel,_signalLevelIcon]];
    
    _deviceIcon.sd_layout
    .widthIs(36)
    .heightIs(36)
    .topSpaceToView(self.contentView, 19)
    .leftSpaceToView(self.contentView, 16);
    
    _deviceName.sd_layout
    .topSpaceToView(self.contentView, 16)
    .leftSpaceToView(_deviceIcon, 16)
    .rightSpaceToView(self.contentView, 46);
    
    _deviceState.sd_layout
    .leftSpaceToView(_deviceName, 6)
    .topSpaceToView(self.contentView, 17.5)
    .rightSpaceToView(self.contentView ,12);
    
    _deviceUUIDTitle.sd_layout
    .widthIs(76)
    .topSpaceToView(_deviceName, 0)
    .leftEqualToView(_deviceName);
    
    _deviceUUID.sd_layout
    .topEqualToView(_deviceUUIDTitle)
    .leftSpaceToView(_deviceUUIDTitle, 0)
    .rightSpaceToView(self.contentView, 12);
    
   _deviceOnlineIcon.sd_layout
    .widthIs(8)
    .heightIs(8)
    .leftEqualToView(_deviceUUIDTitle)
    .topSpaceToView(_deviceUUIDTitle, 2);
    
    _deviceOnline.sd_layout
    .widthIs(28)
    .heightIs(12)
    .leftSpaceToView(_deviceOnlineIcon, 6)
    .topSpaceToView(_deviceUUIDTitle,0);
    
    _signalLevelIcon.sd_layout
    .widthIs(15)
    .heightIs(11)
    .rightSpaceToView(self.contentView, 44)
    .topSpaceToView(_deviceUUIDTitle, 0);
    
   
    
    _signalLevel.sd_layout
    .heightIs(12)
    .leftSpaceToView(_signalLevelIcon, 6)
    .rightSpaceToView(self.contentView, 12)
    .topSpaceToView(_deviceUUIDTitle, 0);
    
   
    
    
    
    
    
    
     [self setupAutoHeightWithBottomView:_signalLevel bottomMargin:10];
    
    
    
    _deviceUUIDTitle.text = @"设备UUID：";
    _signalLevel.text = @"信号";
}


- (void)setModelData:(DeviceListItem *)item {
    _deviceName.text = item.efairydevice_name;
   
    
   
    if(item.efairydevice_is_online){
        _deviceOnline.textColor = ColorFromRGB(0x3FB059);
        _deviceOnline.text = @"在线";
        _deviceOnlineIcon.image = [UIImage imageNamed:@"icon_green_point"];
    }else{
        _deviceOnline.textColor = ColorFromRGB(0xBBBBBB);
        _deviceOnline.text = @"离线";
        _deviceOnlineIcon.image = [UIImage imageNamed:@"icon_gray_point"];
    }
   
    _deviceState.textColor = ColorFromRGB([DeviceListCell getDeviceStateColor:(int)item.efairydevice_alarm_id]);
    _deviceState.text = [DeviceListCell getDeviceState:(int)item.efairydevice_alarm_id];
    
    _deviceUUID.text = item.efairydevice_uuid;
    
    if(!item.efairydevice_is_online){
        _signalLevelIcon.image = [UIImage imageNamed:@"icon_signal_no"];
    }else{
        switch ((int)item.efairydevice_csq_level) {
            case 0:
                _signalLevelIcon.image = [UIImage imageNamed:@"icon_signal_strong"];
                break;
            case 1:
                _signalLevelIcon.image = [UIImage imageNamed:@"icon_signal_weak"];
                break;
            case 2:
                _signalLevelIcon.image = [UIImage imageNamed:@"icon_signal_mediu"];
                break;
            default:
                break;
        }
    }
   
    
    
    
    
    
    
}


@end
